home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / system-tools / reboot / reboot.c < prev    next >
C/C++ Source or Header  |  1996-02-26  |  12KB  |  560 lines

  1. ; /*
  2. CatComp Reboot.cd CFILE Reboot_strings.h NOARRAY NOBLOCK NOCODE
  3. CatComp Reboot.cd CTFILE Reboot_blank.ct
  4. CatComp Reboot.cd Reboot_deutsch.ct CATALOG Catalogs/Deutsch/Reboot.catalog
  5.  
  6. ; DICE
  7. dcc Reboot.c -mS -mi -proto -v -oReboot
  8.  
  9. ; SAS
  10. ; SC RESOPT PARM=REGISTERS UCHAR CONSTLIB STREQ ANSI NOSTKCHK NOICONS OPT OPTPEEP Reboot.c
  11. ; Slink LIB:c.o Reboot.o TO Reboot LIB LIB:sc.lib SC SD
  12. Quit
  13. */
  14.  
  15. /*
  16. **
  17. **  $VER: Reboot.c 1.1 (17.9.95)
  18. **  Reboot 1.0
  19. **
  20. **  main file
  21. **
  22. **  (C) Copyright 1994/1995 by Roland 'Gizzy' Mainz
  23. **          All Rights Reserved
  24. **
  25. */
  26.  
  27. /* We did not want any names before V36 */
  28. #define INTUI_V36_NAMES_ONLY TRUE
  29.  
  30. /* amiga includes */
  31. #include <exec/types.h>
  32. #include <utility/utility.h>
  33. #include <dos/dos.h>
  34. #include <intuition/intuition.h>
  35. #include <workbench/startup.h>
  36. #include <workbench/icon.h>
  37. #include <libraries/locale.h>
  38.  
  39. /* amiga prototypes */
  40. #include <clib/exec_protos.h>
  41. #include <clib/utility_protos.h>
  42. #include <clib/dos_protos.h>
  43. #include <clib/intuition_protos.h>
  44. #include <clib/icon_protos.h>
  45. #include <clib/locale_protos.h>
  46.  
  47. /* ansi includes */
  48. #include <string.h>
  49.  
  50. /* version string */
  51. #include "Reboot_rev.h"
  52.  
  53. /* locale */
  54. #define CATCOMP_NUMBERS
  55. #define CATCOMP_STRINGS
  56. #include "Reboot_strings.h"
  57.  
  58. /* misc defines */
  59. #define NAME "Reboot"
  60.  
  61. /* prototypes */
  62.                 void                  chkabort( void );
  63.                 long                  wbmain( struct WBStartup * );
  64.                 long                  main( long, STRPTR * );
  65.         static  void                  DefaultSettings( void );
  66.         static  void                  FreeInitProjectResult( void );
  67.  
  68.         static  void                  ClearRDA( void );
  69.         static  void                  ScanRDA( void );
  70.         static  void                  ScanToolTypes( STRPTR * );
  71.  
  72.         static  void                  RunTool( void );
  73.         static  BOOL                  OpenLibStuff( void );
  74.         static  void                  CloseLibStuff( void );
  75.         static  BOOL                  CreateBasicResources( void );
  76.         static  void                  DeleteBasicResources( void );
  77.                 void                  AttemptOpenLibrary( struct Library **, STRPTR, STRPTR, ULONG );
  78.         static  void                  RunTool( void );
  79.         static  void                  MyReboot( void );
  80.                 STRPTR                SafeGetCatalogStr( struct Catalog *, LONG, STRPTR );
  81.  
  82.  
  83.  
  84. /* version_string */
  85. STRPTR versionstring = VERSTAG;
  86.  
  87. long main_retval,
  88.      main_retval2;
  89.  
  90. static struct RDArgs  *startuprda;
  91.  
  92. /* shared libraries */
  93. struct Library *UtilityBase,
  94.                *IntuitionBase,
  95.                *IconBase,
  96.                *LocaleBase;
  97.  
  98. /* locale support */
  99. struct Catalog *ct;
  100.  
  101. /* template for ReadArgs() */
  102. #define STARTUP_TEMPLATE "REBOOTDELAY/K/N,QUIET=NOREQ/S"
  103.  
  104. static
  105. struct
  106. {
  107.     long *rebootdelay;
  108.     long *noreq;
  109. } result;
  110.  
  111. static
  112. struct
  113. {
  114.     ULONG rebootdelay;
  115.     ULONG noreq;
  116. } project;
  117.  
  118.  
  119. /* disable CTRL_C break support (DICE CTRL_C abort function) */
  120. void chkabort( void )
  121. {
  122. }
  123.  
  124.  
  125. long wbmain( struct WBStartup *wbstartup )
  126. {
  127.     return( main( 0L, (STRPTR *)wbstartup ) );
  128. }
  129.  
  130.  
  131. long main( long ac, STRPTR *av )
  132. {
  133.     LONG               numArgs,
  134.                        x;
  135.     struct WBStartup  *wbstartup;
  136.     struct WBArg      *wbarg;
  137.     struct DiskObject *tooldobj,
  138.                       *projectdobj;
  139.     BPTR               oldToolLock,
  140.                        oldProjectLock;
  141.  
  142.     x = main_retval2 = 0L;
  143.     main_retval = RETURN_OK;
  144.  
  145.     DefaultSettings();
  146.  
  147. /* Workbench */
  148.     if( ac == 0L )
  149.     {
  150.       wbstartup = (struct WBStartup *)av;
  151.  
  152.       if( CreateBasicResources() )
  153.       {
  154.         numArgs = wbstartup -> sm_NumArgs;
  155.         wbarg   = wbstartup -> sm_ArgList;
  156.  
  157.         if( *(wbarg[ 0 ] . wa_Name) )
  158.         {
  159.           if( wbarg[ 0 ] . wa_Lock )
  160.           {
  161.             oldToolLock = CurrentDir( (wbarg[ 0 ] . wa_Lock) );
  162.           }
  163.  
  164.           if( tooldobj = GetDiskObjectNew( (wbarg[ 0 ] . wa_Name) ) )
  165.           {
  166.             /* two possible cases when started from workbench ... */
  167.             if( numArgs < 2L )
  168.             {
  169.               /* ... first case, only our tool icon is given, create one project here */
  170.  
  171.               ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  172.  
  173.               RunTool();
  174.  
  175.               FreeInitProjectResult();
  176.             }
  177.             else
  178.             {
  179.               /* ... second case, a couple of project icons are given, multiple projects will start from here */
  180.               for( x = 1L ; x < numArgs ; x++ )
  181.               {
  182.                 if( wbarg[ x ] . wa_Lock )
  183.                 {
  184.                   oldProjectLock = CurrentDir( (wbarg[ x ] . wa_Lock) );
  185.                 }
  186.  
  187.                 if( *(wbarg[ x ] . wa_Name) )
  188.                 {
  189.                   if( projectdobj = GetDiskObject( (wbarg[ x ] . wa_Name) ) )
  190.                   {
  191.                     ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  192.                     ScanToolTypes( (STRPTR *)(projectdobj -> do_ToolTypes) );
  193.  
  194.                     RunTool();
  195.  
  196.                     FreeInitProjectResult();
  197.                     DefaultSettings();
  198.  
  199.                     FreeDiskObject( projectdobj );
  200.                   }
  201.                 }
  202.  
  203.                 if( wbarg[ x ] . wa_Lock )
  204.                 {
  205.                   CurrentDir( oldProjectLock );
  206.                 }
  207.               }
  208.             }
  209.  
  210.             FreeDiskObject( tooldobj );
  211.           }
  212.  
  213.           if( wbarg[ 0 ] . wa_Lock )
  214.           {
  215.             CurrentDir( oldToolLock );
  216.           }
  217.         }
  218.  
  219.         DeleteBasicResources();
  220.       }
  221.     }
  222.     else
  223.     {
  224. /* CLI/Shell */
  225.       if( CreateBasicResources() )
  226.       {
  227.         if( startuprda = ReadArgs( STARTUP_TEMPLATE, (LONG *)(&result), NULL ) )
  228.         {
  229.           /* did we get a CTRL_C signal ? */
  230.           if( !CheckSignal( SIGBREAKF_CTRL_C ) )
  231.           {
  232.             ScanRDA();
  233.  
  234.             RunTool();
  235.  
  236.             FreeInitProjectResult();
  237.           }
  238.           else
  239.           {
  240.             main_retval2 = ERROR_BREAK;
  241.             main_retval  = RETURN_WARN;
  242.           }
  243.  
  244.           FreeArgs( startuprda );
  245.         }
  246.         else
  247.         {
  248.           main_retval2 = IoErr();
  249.           main_retval  = RETURN_ERROR;
  250.         }
  251.  
  252.         PrintFault( main_retval2, NAME );
  253.  
  254.         DeleteBasicResources();
  255.       }
  256.     }
  257.  
  258.     SetIoErr( main_retval2 );
  259.  
  260.     return( main_retval );
  261. }
  262.  
  263.  
  264. void DefaultSettings( void )
  265. {
  266.     ClearRDA();
  267.  
  268.     memset( (&project), 0, sizeof(project) );
  269.  
  270.     project . rebootdelay = 2UL;
  271. }
  272.  
  273.  
  274. void ClearRDA( void )
  275. {
  276.     memset( (&result), 0, sizeof(result) );
  277. }
  278.  
  279.  
  280. void ScanRDA( void )
  281. {
  282.     if( result . rebootdelay )
  283.     {
  284.       project . rebootdelay = (ULONG)(*(result . rebootdelay));
  285.     }
  286.  
  287.     project . noreq = result . noreq;
  288.  
  289.     ClearRDA();
  290. }
  291.  
  292.  
  293. void ScanToolTypes( STRPTR *tt )
  294. {
  295.     STRPTR s;
  296.  
  297.     if( s = FindToolType( tt, "REBOOTDELAY" ) )
  298.       (void)StrToLong( s, (LONG *)(&(project . rebootdelay)) ); /* note: result ignored */
  299.  
  300.     if( s = FindToolType( tt, "NOREQ" ) )
  301.       result . noreq = (long *)s;
  302.  
  303.     if( s = FindToolType( tt, "QUIET" ) )
  304.       result . noreq = (long *)s;
  305.  
  306.     ScanRDA();
  307. }
  308.  
  309. static
  310. void FreeInitProjectResult( void )
  311. {
  312.     /* NOP */
  313. }
  314.  
  315.  
  316. BOOL CreateBasicResources( void )
  317. {
  318.     return( OpenLibStuff() );
  319. }
  320.  
  321.  
  322. void DeleteBasicResources( void )
  323. {
  324.     CloseLibStuff();
  325. }
  326.  
  327.  
  328. BOOL OpenLibStuff( void )
  329. {
  330.     if( IntuitionBase = OpenLibrary( "intuition.library", 37UL ) )
  331.     {
  332.       AttemptOpenLibrary( (&UtilityBase),  NAME, UTILITYNAME,      37UL );
  333.       AttemptOpenLibrary( (&IconBase),     NAME, ICONNAME,         37UL );
  334.       AttemptOpenLibrary( (&LocaleBase),   NAME, "locale.library", 38UL );
  335.  
  336.       if( LocaleBase )
  337.       {
  338.         ct = OpenCatalogA( NULL, "Reboot.catalog", NULL );
  339.       }
  340.  
  341.       if( IconBase )
  342.       {
  343.         return( TRUE );
  344.       }
  345.     }
  346.  
  347.     CloseLibStuff();
  348.  
  349.     return( FALSE );
  350. }
  351.  
  352.  
  353. void CloseLibStuff( void )
  354. {
  355.     if( LocaleBase )
  356.     {
  357.       CloseCatalog( ct );
  358.  
  359.       CloseLibrary( LocaleBase );
  360.     }
  361.  
  362.     CloseLibrary( IconBase );
  363.     CloseLibrary( UtilityBase );
  364.     CloseLibrary( IntuitionBase );
  365. }
  366.  
  367.  
  368. void AttemptOpenLibrary( struct Library **library, STRPTR title, STRPTR libname, ULONG libversion )
  369. {
  370.     struct EasyStruct LibNotFoundES =
  371.     {
  372.       sizeof(struct EasyStruct),
  373.       0,
  374.       title,
  375.       SafeGetCatalogStr( ct, MSG_LIB_NOT_FOUND_REQTEXT, MSG_LIB_NOT_FOUND_REQTEXT_STR ),
  376.       SafeGetCatalogStr( ct, MSG_LIB_NOT_FOUND_REQGAD,  MSG_LIB_NOT_FOUND_REQGAD_STR )
  377.     };
  378.  
  379.     struct EasyStruct LibWrongVersionES =
  380.     {
  381.       sizeof(struct EasyStruct),
  382.       0,
  383.       title,
  384.       SafeGetCatalogStr( ct, MSG_LIB_WRONG_VERSION_REQTEXT, MSG_LIB_WRONG_VERSION_REQTEXT_STR ),
  385.       SafeGetCatalogStr( ct, MSG_LIB_WRONG_VERSION_REQGAD,  MSG_LIB_WRONG_VERSION_REQGAD_STR )
  386.     };
  387.  
  388.     if( (*library) == NULL )
  389.     {
  390.       for( ;; )
  391.       {
  392.         /* attemp to open shared library */
  393.         (*library) = OpenLibrary( libname, 0UL );
  394.  
  395.         if( *library )
  396.         {
  397.           if( ((*library) -> lib_Version) < libversion )
  398.           {
  399.             EasyRequest( NULL, (&LibWrongVersionES), NULL, libname, libversion );
  400.  
  401.             CloseLibrary( (*library) );
  402.             (*library) = NULL;
  403.           }
  404.  
  405.           break;
  406.         }
  407.  
  408.         /* prompt the user */
  409.         if( EasyRequest( NULL, (&LibNotFoundES), NULL, libname ) == 0L )
  410.         {
  411.           /* user canceled */
  412.           break;
  413.         }
  414.       }
  415.     }
  416. }
  417.  
  418.  
  419. void RunTool( void )
  420. {
  421.     struct EasyStruct RebootES =
  422.     {
  423.       sizeof(struct EasyStruct),
  424.       0,
  425.       NAME,
  426.       SafeGetCatalogStr( ct, MSG_REBOOT_REQTEXT, MSG_REBOOT_REQTEXT_STR ),
  427.       SafeGetCatalogStr( ct, MSG_REBOOT_REQGAD,  MSG_REBOOT_REQGAD_STR )
  428.     };
  429.  
  430.     if( project . noreq )
  431.     {
  432.       MyReboot();
  433.     }
  434.     else
  435.     {
  436.       if( EasyRequest( NULL, (&RebootES), NULL ) == 1L )
  437.       {
  438.         /* user wants to reboot */
  439.         MyReboot();
  440.       }
  441.       else
  442.       {
  443.         main_retval2 = ERROR_BREAK;
  444.         main_retval  = RETURN_WARN;
  445.       }
  446.     }
  447. }
  448.  
  449.  
  450. void MyReboot( void )
  451. {
  452.     struct Window *abortreqwindow;
  453.     ULONG          x;
  454.     BOOL           abort;
  455.     ULONG          abortreqsignal,
  456.                    sigmask,
  457.                    signals;
  458.  
  459.     abort = FALSE;
  460.  
  461.     struct EasyStruct AbortRebootES =
  462.     {
  463.       sizeof(struct EasyStruct),
  464.       0,
  465.       NAME,
  466.       SafeGetCatalogStr( ct, MSG_ABORTREBOOT_REQTEXT, MSG_ABORTREBOOT_REQTEXT_STR )
  467.       SafeGetCatalogStr( ct, MSG_ABORTREBOOT_REQGAD,  MSG_ABORTREBOOT_REQGAD_STR )
  468.     };
  469.  
  470.     if( (project . rebootdelay) < 1UL )
  471.     {
  472.       project . rebootdelay = 1UL;
  473.     }
  474.  
  475.     if( project . noreq )
  476.     {
  477.       abortreqwindow = NULL;
  478.     }
  479.     else
  480.     {
  481.       abortreqwindow = BuildEasyRequestArgs( NULL, (&AbortRebootES), 0UL, NULL );
  482.     }
  483.  
  484.     abortreqsignal = ((abortreqwindow)?(1UL << abortreqwindow -> UserPort -> mp_SigBit):(0UL));
  485.  
  486.     sigmask = SIGBREAKF_CTRL_C | abortreqsignal;
  487.  
  488.     /* For each second we'll check if any abort signal arrived (CTRL_C or requester) */
  489.     for( x = 0UL ; (x < (project . rebootdelay)) ; x++ )
  490.     {
  491.       /* check if user aborts countdown */
  492.       signals = CheckSignal( sigmask );
  493.  
  494.       if( signals & SIGBREAKF_CTRL_C )
  495.       {
  496.         abort = TRUE;
  497.       }
  498.  
  499.       /* no window, no signal, no event processing ... */
  500.       if( abortreqsignal )
  501.       {
  502.         if( signals & abortreqsignal )
  503.         {
  504.           if( SysReqHandler( abortreqwindow, NULL, FALSE ) == 0L )
  505.           {
  506.             abort = TRUE;
  507.           }
  508.         }
  509.       }
  510.  
  511.       if( abort )
  512.       {
  513.         break;
  514.       }
  515.  
  516.       Delay( TICKS_PER_SECOND );
  517.     }
  518.  
  519.     /* Check if user has selected test mode ("Setenv Reboot OFF")*/
  520.     if( abort == FALSE )
  521.     {
  522.       TEXT buff[ 256 ];
  523.  
  524.       if( GetVar( NAME, buff, 255L, 0UL ) != (-1L) )
  525.       {
  526.         /* may be replaced by ReadArgs() */
  527.         if( !Stricmp( buff, "OFF" ) )
  528.         {
  529.           abort = TRUE;
  530.         }
  531.       }
  532.     }
  533.  
  534.     if( abort )
  535.     {
  536.       main_retval2 = ERROR_BREAK;
  537.       main_retval  = RETURN_WARN;
  538.     }
  539.     else
  540.     {
  541.       /* bite in the dust */
  542.       ColdReboot();
  543.     }
  544.  
  545.     /* user has aborted the rebooting sequence */
  546.     FreeSysRequest( abortreqwindow );
  547. }
  548.  
  549.  
  550. STRPTR SafeGetCatalogStr( struct Catalog *ct, LONG id, STRPTR s )
  551. {
  552.     if( LocaleBase )
  553.     {
  554.       s = GetCatalogStr( ct, id, s );
  555.     }
  556.  
  557.     return( s );
  558. }
  559.  
  560.